package baiqitun.stupro.api.controller;

import baiqitun.stupro.api.model.UserInfo;
import baiqitun.stupro.common.enums.BooleanEnum;
import baiqitun.stupro.common.model.Result;
import baiqitun.stupro.common.util.AuthUtil;
import baiqitun.stupro.common.util.ResultUtil;
import baiqitun.stupro.common.validate.group.SaveGroup;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserService;
import baiqitun.stupro.subject.entity.SysTutorInfo;
import baiqitun.stupro.subject.service.SysTutorInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kedaji
 */
@Api(description = "导师控制器")
@RequestMapping("/tutorInfo")
@RestController
public class TutorInfoController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysTutorInfoService sysTutorInfoService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("将一名新的导师移动到指定的科目")
    @PostMapping
    public Result<?> saveToSubject(Authentication authentication, String tutorUserId, String subjectId){
        String username = AuthUtil.getUsername(authentication);
        sysTutorInfoService.moveTutorToOtherSubject(tutorUserId, subjectId, username);
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("同意学生的【选择导师】请求")
    @PutMapping("/selectTutorRequest/agree")
    public Result<?> agreeTaskForSelectTutorRequest(@ApiParam("任务id") String taskId){
        sysTutorInfoService.handleTaskForSelectTutor(taskId, BooleanEnum.TRUE.getValue());
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("拒绝学生的【选择导师】请求")
    @PutMapping("/selectTutorRequest/deny")
    public Result<?> denyTaskForSelectTutorRequest(@ApiParam("任务id") String taskId){
        sysTutorInfoService.handleTaskForSelectTutor(taskId, BooleanEnum.FALSE.getValue());
        return ResultUtil.ok();
    }

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation("获取等待导师处理的任务")
    @GetMapping("/selectTutorRequest")
    public Result<List<Task>> listTaskForSelectTutorRequest(Authentication authentication){
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        SysUser user = sysUserService.getUserByUsername(userDetails.getUsername());
        List<Task> tasks = sysTutorInfoService.listTaskForSelectTutorRequest(user.getId());
        return ResultUtil.ok(tasks);
    }
}
